package com.jingdianjichi.auth.application.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.application.convert.AuthPermissionDTOConverter;
import com.jingdianjichi.auth.application.convert.AuthRoleDTOConverter;
import com.jingdianjichi.auth.application.dto.AuthPermissionDTO;
import com.jingdianjichi.auth.application.dto.AuthRoleDTO;
import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.domain.entity.AuthPermissionBO;
import com.jingdianjichi.auth.domain.entity.AuthRoleBO;
import com.jingdianjichi.auth.domain.service.AuthPermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/permission/")
@Slf4j
public class PermissionController {
    @Resource
    private AuthPermissionDomainService authPermissionDomainService;


    @PostMapping("add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO)  {
        if(log.isInfoEnabled()){
            log.info("新增权限信息:{}", JSON.toJSONString(authPermissionDTO));
        }
        try {
            //断言参数校验Precondition
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getName()),"权限名称不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getPermissionKey()),"权限key不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(),"角色名不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getType(),"角色类型不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getStatus(),"角色状态不能为空");
            AuthPermissionBO authPermissionBO= AuthPermissionDTOConverter.Instance.convertDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.add(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.add.error:{}",e.getMessage(),e);
            return Result.fail("新增角色失败");
        }
    }

    @PostMapping("update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO){
        if(log.isInfoEnabled()){
            log.info("更新权限信息:{}", JSON.toJSONString(authPermissionDTO));
        }
        try {
            //断言参数校验Precondition
            Preconditions.checkNotNull(authPermissionDTO.getId(),"权限id不能为空");
            AuthPermissionBO authPermissionBO= AuthPermissionDTOConverter.Instance.convertDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.update(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.update.error:{}",e.getMessage(),e);
            return Result.fail("更新权限信息失败");
        }
    }

    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        if(log.isInfoEnabled()){
            log.info("删除权限信息:{}", JSON.toJSONString(authPermissionDTO));
        }
        try {
            //断言参数校验Precondition
            Preconditions.checkNotNull(authPermissionDTO.getId(),"权限id不能为空");
            AuthPermissionBO authPermissionBO= AuthPermissionDTOConverter.Instance.convertDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.delete(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.update.error:{}",e.getMessage(),e);
            return Result.fail("删除权限失败");
        }
    }


}
